from .global_configure import ConfigureFile
from datetime import timedelta

# BROKER_BACKEND = 'pyamqp'
# BROKER_URL = 'pyamqp://admin:123456@192.168.1.5//'

get_connect_configure = ConfigureFile()
BROKER_BACKEND = 'redis'
BROKER_URL = get_connect_configure.get_configure_file('broker_redis', '') + '3'
CELERY_RESULT_BACKEND = get_connect_configure.get_configure_file('broker_redis', '') + '4'
CELERY_IMPORT = {
    'automationreport.celery_tasks',
}

# 定义不同的消息队列,timed_tasks 定时任务 timely_tasks及时执行任务
CELERY_QUEUES = {
    'timed_tasks': {
        'exchange': 'timed_tasks',
        'exchange_type': 'direct',
        'binding_key': 'timed_tasks',
    },
    'timely_tasks': {
        'exchange': 'timely_tasks',
        'exchange_type': 'direct',
        'binding_key': 'timely_tasks',
    }
}

# 默认任务的队列
CELERY_DEFAULT_QUEUE = 'timely_tasks'

# # 指定任务序列化方式
# CELERY_TASK_SERIALIZER = 'json'

# 指定结果序列化方式
CELERY_RESULT_SERIALIZER = 'json'
#
# # 指定任务接受的序列化类型
# CELERY_ACCEPT_CONTENT = ['json']

# 设置时区
CELERY_TIMEZONE = 'Asia/Shanghai'

# 防止任务锁死
CELERY_FORCE_EXECV = True

# 设置兵法的worker数量
CELERY_CONCURRENCY = 4

# 允许重试机制
CELERY_ACKS_LATE = True

# 设置每个worker最多执行100个任务就销毁,防止内存泄露
CELERY_MAX_TASKS_PER_CHILD = 100

# 设置超时时间
CELERY_TASKS_TIME_LIMIT = 12 * 30

# 定时任务
CELERYBEAT_SCHEDULE = {
    'timing_timing_update_data': {
        'task': 'automationquery.celery_tasks.timing_update_data',
        'schedule': timedelta(seconds=0.5 * 60 * 60),
        'options': {
            'queue': 'timed_tasks'
        }
    }
}
